package com.freshcity.page.friend
{
	import com.freshcity.component.container.Component;
	import com.freshcity.core.OpCodes;
	import com.freshcity.manager.LayerManager;
	import com.freshcity.utils.StringUtil;
	
	import fl.data.DataProvider;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.ui.Keyboard;

	public class RejectRequest extends Component
	{
		private var back:RejectWin;
		private var backMask:Sprite;
		private var toUser:String;

		public function RejectRequest(user:String)
		{
			this.toUser = user;
			
			var parent:DisplayObjectContainer=LayerManager.msgTipLayer;
			super(parent, 0, 0);
			this.x=(stage.width - back.width) / 2;
			this.y=(stage.height - back.height) / 2;

			back.cancelBtn.addEventListener(MouseEvent.CLICK, hide);
			back.closeBtn.addEventListener(MouseEvent.CLICK, hide);
			back.submitBtn.addEventListener(MouseEvent.CLICK, onSubmit);

			back.wordsCombox.addEventListener(Event.CHANGE, onWordsComboxHandler);
		}
		
		protected function onKeyDownHandler(event:KeyboardEvent):void
		{
			if (event.keyCode == Keyboard.ENTER)
			{				
				onSubmit();
			}
		}
		
		protected function onWordsComboxHandler(event:Event):void
		{
			var words:String=back.wordsCombox.selectedLabel;
			back.contentArea.text="谢谢你的心意," + words;
		}

		protected function onSubmit(event:Event=null):void
		{
			var s:String=StringUtil.trim(back.contentArea.text);
			var loc1:int=StringUtil.getANSILength(s);

			if (loc1 < 2)
			{
				main.msg.showRollTipsMsg("内容过于简单");
			}
			else if (loc1 > 100)
			{
				main.msg.showRollTipsMsg("内容太多啦");
			}
			else
			{
				main.commu.send(OpCodes.WRITE_MSG, {t: this.toUser, c: s, r: true}, onWriteMsg);
				back.submitBtn.enabled=false;
			}
		}
		
		private function onWriteMsg(data:*):void
		{
			main.commu.unRegister(OpCodes.WRITE_MSG);
			this.dispatchEvent(new Event(Event.COMPLETE));
			
			if(data is String){
				main.msg.showRollTipsMsg(data);
			}else{
				var msg:String;
				if(data is int){
					msg = "发送成功。今日可主动留言数减少为"+data+"条。";
				}else{
					msg = "信息已成功发出";
				}
				
				main.msg.showRollTipsMsg(msg);
				this.hide();
			}
		}

		override protected function addChildren():void
		{
			super.addChildren();
			back=new RejectWin();
			addChild(back);

			var da:Array=[
				{label: '你能送些清水给我吗？'},
				{label: '我喜欢玫瑰更多些！'},
				{label: '你能连续七天在睡觉前留言和我说晚安吗？'},
				{label: '你能在我梧桐树的情意排名中一直保持第一吗？'},
				{label: '你能回答我一个问题吗？'},
				{label: '给点时间，让我们了解多一点，好吗？'},
				{label: '你敢投个纸飞机公开你对我的感觉吗？'}];

			back.wordsCombox.dataProvider=new DataProvider(da);
		}

		override public function hide(event:Event=null):void
		{
			if (backMask.parent)
				backMask.parent.removeChild(backMask);
			super.hide(event);
		}

		override public function show(event:Event=null):void
		{
			backMask=new Sprite();
			backMask.graphics.clear();
			backMask.graphics.beginFill(0xffffff, 0);
			backMask.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
			backMask.graphics.endFill();
			parent.addChildAt(backMask, 0);

			back.wordsCombox.selectedIndex=0;
			back.wordsCombox.dispatchEvent(new Event(Event.CHANGE));
			
			super.show(event);
			main.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDownHandler);
		}

		override public function destroy():void
		{
			back.cancelBtn.removeEventListener(MouseEvent.CLICK, hide);
			back.closeBtn.removeEventListener(MouseEvent.CLICK, hide);
			back.submitBtn.removeEventListener(MouseEvent.CLICK, onSubmit);
			back.wordsCombox.removeEventListener(Event.CHANGE, onWordsComboxHandler);
			main.stage.removeEventListener(KeyboardEvent.KEY_DOWN, onKeyDownHandler);
			super.destroy();
		}

	}
}
